/**
 * windowPop 弹窗 
 * 调用格式
 * popJson = {
		popContent :'',
		cancelClass :'按钮1的class',
		cancelHtml:'按钮1的内容',
		confirmClass :'按钮2的class',
		confirmHtml:'按钮2的内容',
	};
 */
;function windowPop(popJson){
	if($('.popPay').length !== 0){
		$('.popPay').remove();
	}
	var oMask = $('.mask').length !== 0 ? $('.mask') : $('<div></div>').attr('class','mask').appendTo('body')
	, popWindow = $('<section></section>').attr('class','pop-window').css('animation','pop-open .4s').appendTo('body')
	, buttonWrap = $('<div></div>')
	,btnWrapContent
	,popTimer
	,dfd = $.Deferred()
	,confirmed
	,cancelClass =  popJson.cancelClass || 'pop-window-cancel'
	,confirmClass = popJson.confirmClass || 'pop-window-confirm'
	,cancelHtml = popJson.cancelHtml || '取消'
	,confirmHtml = popJson.confirmHtml || '确定'
	;
	//底部按钮内容
	btnWrapContent = '<button class="' + cancelClass + '">'+ cancelHtml +'</button>' +
					 '<button class="' + confirmClass + '">'+ confirmHtml +'</button>';
	buttonWrap.html(btnWrapContent)  //底部按钮内容添加到控件
	popWindow.html(popJson.popContent);  //弹窗内容添加
	buttonWrap.appendTo(popWindow);	//把底部按钮添加到弹窗
	//控制弹窗的位置
	adjustPosition(popWindow);
	$(window).resize(function(){ adjustPosition(popWindow)});
	
	//确定取消按钮
	var confirmBtn = $('.'+ confirmClass)
	,cancelBtn = $('.'+ cancelClass)
	;
	//监听按钮点击
	popTimer = setInterval(function(){
		if(confirmed !== undefined ){
			dfd.resolve(confirmed);
			clearInterval(popTimer);
		}
	},50);
	//按钮点击事件
	confirmBtn.click(function(){ 
		confirmed = true;
		closePop(); 
		
	});
	cancelBtn.click(function(){ 
		confirmed = false;
		closePop(); 
	});
	function closePop(){
		//popWindow.css('animation','pop-close .3s');
		oMask.remove();
		/*setTimeout(function(){ */
			popWindow.remove();
		/* },300);*/
	};
    return dfd.promise();
};

/**
 * 调整弹层位置
 */
function adjustPosition(obj){
	var screenHeight = $(window).height()
	, popTop =  (screenHeight*0.85 - obj.innerHeight())/2	
	;
	obj.css({ top : popTop});
};